'use strict';
import Promise from './es6-promise.min'

module.exports = {
    // root_url:'https://checkorder2.test.mejour.cn/checkorder',
    // root_url:'https://checkorder-ceshi.mejour.cn/checkorder',
     root_url: "http://192.168.110.160:9003/checkorder",
  get(url, data) {
    return new Promise((resolve, reject) => {
      wx.request({
        url: this.root_url + url + "?token=" + wx.getStorageSync("token"),
        method: 'GET',
        data: data,
        headers: {
          'Content-Type': 'application/json'
        },
        success: function (res) {
          res = res.data;
          if (res.key === 1000) {
            resolve(res)
          } else if (res.key === 1001) {
            resolve(res)
            wx.removeStorageSync("token");
            wx.removeStorageSync("userInfo");
            wx.reLaunch({
              url: '/pages/index/index',
            });
          } else {
            resolve(res)
            wx.showToast({
              title: res.message,
              icon: "none"
            });
          }
        },
        fail: function (res) {
          reject(res)
        }
      })
    })
  },

  
  post(url, data) {
    return new Promise((resolve, reject) => {
      wx.request({
        url: this.root_url + url + "?token=" + wx.getStorageSync("token"),
        data: data,
        method: 'POST',
        // headers: {
        //   'Content-Type': 'application/json'
        // },
        // header: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' },
        success: function (res) {
          res = res.data;
          if (res.key === 1000) {
            resolve(res)
          } else if (res.key === 1001) {
            resolve(res)
            wx.removeStorageSync("token");
            wx.removeStorageSync("userInfo");
            wx.reLaunch({
              url: '/pages/index/index',
            });
          } else {
            resolve(res)
            wx.showToast({
              title: res.message,
              icon: "none"
            });
          }
        },
        fail: function (res) {
          reject(res)
        }
      })
    })
  },


  // content-type不同
  postUTF8(url, data) {
    return new Promise((resolve, reject) => {
      wx.request({
        url: this.root_url + url + "?token=" + wx.getStorageSync("token"),
        data: data,
        method: 'POST',
        // headers: {
        //   'Content-Type': 'application/json'
        // },
        header: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' },
        success: function (res) {
          res = res.data;
          if (res.key === 1000) {
            resolve(res)
          } else if (res.key === 1001) {
            resolve(res)
            wx.removeStorageSync("token");
            wx.removeStorageSync("userInfo");
            wx.reLaunch({
              url: '/pages/index/index',
            });
          } else {
            resolve(res)
            wx.showToast({
              title: res.message,
              icon: "none"
            });
          }
        },
        fail: function (res) {
          reject(res)
        }
      })
    })
  },

  json2Form(json) {
    var str = []
    for (var p in json) {
      str.push(encodeURIComponent(p) + "=" + encodeURIComponent(json[p]))
    }
    return str.join("&")
  }

};


